package reback;

public class item200 {
    public static int numIslands(char[][] grid) {
        if(grid.length==0)
            return 0;
        int row =grid.length;
        int col = grid[0].length;
        int count=0;
        for (int i = 0; i <row ; i++) {
            for (int j = 0; j <col ; j++) {
                if(grid[i][j]=='0')
                    continue;
                count=count+ reback(grid,i,j);
            }
        }
        return count;
    }

    private static int reback(char[][] grid, int i, int j) {
        if(i<0||j<0||j>=grid[0].length||i>=grid.length) return 1;
        if(grid[i][j]=='0')
            return 1;
        if(i<grid.length&&j<grid[0].length)
        {
            grid[i][j]='0';
            reback(grid,i+1,j);
            reback(grid,i-1,j);
            reback(grid,i,j+1);
            reback(grid,i,j-1);

        }
        return 1;
    }

}
